<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/jsp/component/common.jsp" %>
<!DOCTYPE html>
<html>
<head>
    <title>医院新增</title>
    <meta charset="utf-8">
    <!-- Bootstrap Core CSS -->
    <link href="${css}/bootstrap.min.css" rel="stylesheet"/>
    <!-- DataTables CSS -->
    <link href="${css}/dataTables.bootstrap.css" rel="stylesheet">
    <!-- DataTables Responsive CSS -->
    <link href="${css}/dataTables.responsive.css" rel="stylesheet">
    <!-- button -->
    <link rel="stylesheet" type="text/css" href="${assets}/button/css/btn.css"/>

    <!-- jQuery -->
    <script src="${js}/jquery-1.9.1.min.js"></script>
    <!-- Bootstrap Core JavaScript -->
    <script src="${js}/bootstrap.min.js"></script>
    <!-- DataTables JavaScript -->
    <script src="${js}/jquery.dataTables.js"></script>
    <script src="${js}/dataTables.bootstrap.js"></script>
    <script src="${js}/bootbox.js"></script>
    <!-- button -->
    <script src="${assets}/button/js/btn.js"></script>
</head>
<body style="width: 98%">
<div class="row">
    <div class="col-lg-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                医院名称:<input type="text" id="hospName" name="hospName"/>
                <button id="query" class="mk_butt" >查询</button>
                <%--<button id="query" type="button" class="btn btn-outline btn-success">查询</button>--%>
            </div>
            <!-- /.panel-heading -->
            <div class="panel-body">
                <div class="dataTable_wrapper">
                    <table id="example" class="table table-striped table-bordered table-hover" width="100%">
                        <thead>
                        <tr align="left">
                            <th><input id="all_id" type="checkbox"></th>
                            <th>医院名称</th>
                            <th>医院等级</th>
                            <th>医院地址</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
            <!-- /.panel-body -->
        </div>
        <!-- /.panel -->
    </div>
    <!-- /.col-lg-12 -->
</div>

<script type="text/javascript">
    var table;
    function initTable() {
        table = $("#example").DataTable({
            "dom": "<'row'<'col-xs-2'l><'#mytool.col-xs-10'><'col-xs-6'f>r>" +
            "t" +
            "<'row'<'col-xs-6'i><'col-xs-6'p>>",
            "bFilter": false,//去掉搜索框
            "bAutoWidth": true, //自适应宽度
            "sPaginationType" : "full_numbers",
            "lengthMenu": [[5, 10], [5, 10]],
//                "bDestroy" : true,
//              "asStripeClasses": [ 'strip1', 'strip2', 'strip3' ],//设置斑马条（奇偶行）的css class
            "ordering": false,//是否允许Datatables开启排序
            "bProcessing" : true,
            "bServerSide" : true,
            "sAjaxSource" : "${URL_ADM_HOSP_SEARCHALL}",
            "sAjaxDataProp": "dataList",//查询后，返回的集合
            "fnServerData": function ( sSource, aoData, fnCallback ) {  //查询条件
                aoData.push(
                        { "name": "hospName", "value": $("#hospName").val() }
                );
                $.ajax( {
                    "dataType": 'json',
                    "url": sSource,
                    "type":'post',
                    "data": aoData,
                    "success": fnCallback
                } );

            },
            "aoColumns" : [ {
                "targets": -1,
                "data": null,
                "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                    $(nTd).html('' +
                            '&nbsp;&nbsp;<input name="id" value="'+oData.id+'" type="checkbox" onClick="isCheckedAll()">' +
                            '');
                }
            }, {
                "mDataProp" : "hospName",
                'sClass': "text-center"
            }, {
                "mDataProp" : "hosLevel",
                "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                    var levelName = "";
                    <c:forEach items="${hospLvlList}" var="level">
                    if('${level.code}'==sData){
                        levelName = "${level.name}";
                    }
                    </c:forEach>
                    $(nTd).html(levelName);
                }
            }, {
                "mDataProp" : "hospAddress",
            }
            ],
            "oLanguage" : {
                "sProcessing" : "正在加载中......",
                "sLengthMenu" : "每页显示 _MENU_ 条记录",
                "sZeroRecords" : "没有数据！",
                "sEmptyTable" : "无数据存在！",
                "sInfo" : "当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录",
                "sInfoEmpty" : "显示0到0条记录",
                "sInfoFiltered" : "数据表中共为 _MAX_ 条记录",
                //"sSearch" : "搜索",
                "oPaginate" : {
                    "sFirst" : "首页",
                    "sPrevious" : "上一页",
                    "sNext" : "下一页",
                    "sLast" : "末页"
                }
            }
        });
    }

    //添加医院关联
    function addRelate(){
        var checkedArray = getCheckedArray();
        for(var i=0; i<checkedArray.length;i++){
//            alert(checkedArray[i].id+"------"+checkedArray[i].hospName);
            var levelName = "";
            <c:forEach items="${hospLvlList}" var="level">
            if('${level.code}'==checkedArray[i].hosLevel){
                levelName = "${level.name}";
            }
            </c:forEach>
            window.parent.addHospDiv(checkedArray[i].id,checkedArray[i].hospName,levelName,checkedArray[i].hospAddress);
            window.parent.closeModal("addRelate");
        }

    }

    //多选按钮
    $("#all_id").click(function(){
        if(this.checked){
            $("input[name='id']").each(function(){
                $(this)[0].checked = true;
            });
        }else{
            $("input[name='id']").each(function(){
                $(this)[0].checked = false;
            });
        }
    });

    //判断是否已经全选
    function isCheckedAll(){
        var isCheckAll = true;
        $("input[name='id']").each(function(){
            if(!this.checked){
                isCheckAll = false;
            }
        });
        if(isCheckAll){
            $("#all_id")[0].checked = true;
        }else{
            $("#all_id")[0].checked = false;
        }
    }

    function getCheckedArray(){
        var checkedArray = [];
        $("input[name='id']").each(function(){
            if(this.checked){
                checkedArray.push(table.row($(this).parents('tr') ).data());
            }
        });
        return checkedArray;
    }

    //关闭弹出框
    function closeModal(flag){
        $('#'+flag+"Modal").modal('hide');
        refreshTable();
    }

    //查询
    $("#query").click(function() {
        refreshTable();
    });

    //刷新table数据
    function refreshTable(){
        table.ajax.reload();
        //重置全选按钮
        $("#all_id")[0].checked = false;
    }

    $(document).ready(function() {
        initTable();
    });

</script>
</body>
</html>